// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <gtest/gtest.h>
#include <pollux/common/base/fs.h>
#include <pollux/common/file/file_systems.h>
#include <tests/dwio/text/writer/file_reader_util.h>
#include <pollux/dwio/text/writer/text_writer.h>
#include <pollux/common/file/temp_directory_path.h>
#include <pollux/testing/vector/vector_test_base.h>

namespace kumo::pollux::text {
    class BufferedWriterSinkTest : public testing::Test,
                                   public pollux::VectorBuilder {
    public:
        void SetUp() override {
            pollux::filesystems::registerLocalFileSystem();
            dwio::common::LocalFileSink::registerFactory();
            rootPool_ = memory::memoryManager()->addRootPool("BufferedWriterSinkTest");
            leafPool_ = rootPool_->addLeafChild("BufferedWriterSinkTest");
            tempPath_ = exec::test::TempDirectoryPath::create();
        }

    protected:
        static void SetUpTestCase() {
            memory::MemoryManager::testingSetInstance({});
        }

        std::shared_ptr<memory::MemoryPool> rootPool_;
        std::shared_ptr<memory::MemoryPool> leafPool_;
        std::shared_ptr<exec::test::TempDirectoryPath> tempPath_;
    };

    TEST_F(BufferedWriterSinkTest, write) {
        auto filePath = fs::path(
            fmt::format("{}/test_buffered_writer.txt", tempPath_->getPath()));
        auto sink = std::make_unique<dwio::common::LocalFileSink>(
            filePath, dwio::common::FileSink::Options{.pool = leafPool_.get()});
        auto bufferedWriterSink = std::make_unique<BufferedWriterSink>(
            std::move(sink), rootPool_->addLeafChild("bufferedWriterSinkTest"), 15);
        bufferedWriterSink->write("hello world", 10);
        bufferedWriterSink->write("this is writer", 10);
        bufferedWriterSink->close();
        std::string result = readFile(filePath);
        EXPECT_EQ(result.size(), 20);
    }

    TEST_F(BufferedWriterSinkTest, abort) {
        auto filePath =
                fs::path(fmt::format("{}/test_buffered_abort.txt", tempPath_->getPath()));
        auto sink = std::make_unique<dwio::common::LocalFileSink>(
            filePath, dwio::common::FileSink::Options{.pool = leafPool_.get()});
        auto bufferedWriterSink = std::make_unique<BufferedWriterSink>(
            std::move(sink), rootPool_->addLeafChild("bufferedWriterSinkTest"), 15);
        bufferedWriterSink->write("hello world", 10);
        bufferedWriterSink->write("this is writer", 10);
        bufferedWriterSink->abort();
        std::string result = readFile(filePath);
        EXPECT_EQ(result.size(), 10);
    }
} // namespace kumo::pollux::text
